package com.sc.nft.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.sc.nft.dao.IntegralTaskDao;
import com.sc.nft.dao.UserReceiveIntegralDao;
import com.sc.nft.entity.IntegralTask;
import com.sc.nft.entity.UserAssets;
import com.sc.nft.entity.UserReceiveIntegral;
import com.sc.nft.enums.AssetChangeLogEnum;
import com.sc.nft.enums.CoinCategoryEnum;
import com.sc.nft.enums.IntegralTaskEnum;
import com.sc.nft.service.UserAssetsService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.sc.nft.service.UserReceiveIntegralService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * 服务接口实现
 *
 * @author rdg
 * @since 2022-05-30 10:42:05
 * @description 由 Mybatisplus Code Generator 创建
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class UserReceiveIntegralServiceImpl implements UserReceiveIntegralService {
    @Resource  UserReceiveIntegralDao userReceiveIntegralDao;
    @Resource
    UserAssetsService userAssetsService;
    @Resource
    IntegralTaskDao integralTaskDao;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void completeOtherIntegralTask(Long userId, IntegralTaskEnum integralTaskEnum, AssetChangeLogEnum assetChangeLogEnum) {
        //查询该任务是否开启
        IntegralTask integralTask= integralTaskDao.getOneTask(integralTaskEnum.getCode());
        //查询用户是否已完成了该任务
        UserReceiveIntegral userReceiveIntegral= userReceiveIntegralDao.getUserReceiveIntegral(userId,integralTaskEnum.getCode());
        //当该任务是启用状态且用户没有该任务的完成记录再添加积分添加积分任务记录
        if (ObjectUtil.isNotEmpty(integralTask)){
            //可以多次完成的任务且已经完成过该任务,修改完成记录表中的积分数量
            if (integralTask.getIsRepeat()&&ObjectUtil.isNotEmpty(userReceiveIntegral)){
                Long code = IntegralTaskEnum.INVITE_NEW_USER.getCode();
                //邀请好友送积分任务,发放三十次后就不再发放
//                if (code.equals(integralTask.getId())&&userReceiveIntegral.getReceiveCount()>=30){
//                    return;
//                }
                //添加积分
                Boolean aBoolean = userAssetsService.addIntegralByUserId(integralTask.getIntegralNum(), userId, CoinCategoryEnum.INTEGRAL, assetChangeLogEnum,userReceiveIntegral.getId(),integralTask.getIntegralTaskName());
                UpdateWrapper<UserReceiveIntegral> wrapper=new UpdateWrapper<>();
                wrapper.set("modify_time",new Date());
                wrapper.setSql("integral_num = integral_num + "+integralTask.getIntegralNum());
                wrapper.setSql("receive_count = receive_count + " +1);
                wrapper.eq("user_id",userId).eq("integral_task_id",integralTask.getId());
                wrapper.eq("is_delete",Boolean.FALSE);
                userReceiveIntegralDao.update(wrapper);
            }else {
                if (ObjectUtil.isEmpty(userReceiveIntegral)){

                        UserReceiveIntegral receiveIntegral = new UserReceiveIntegral();
                        receiveIntegral.setUserId(userId);
                        receiveIntegral.setIntegralNum(integralTask.getIntegralNum());
                        receiveIntegral.setIntegralTaskId(integralTask.getId());
                        receiveIntegral.setIsReceive(true);
                        receiveIntegral.setReceiveCount(1);
                        receiveIntegral.setReceiveInformation(integralTask.getReceiveInformation());
                        receiveIntegral.setCreateTime(new Date());
                        receiveIntegral.setIsDelete(Boolean.FALSE);
                         receiveIntegral.insert();
                    //添加积分
                    Boolean flag = userAssetsService.addIntegralByUserId(integralTask.getIntegralNum(), userId, CoinCategoryEnum.INTEGRAL, assetChangeLogEnum,receiveIntegral.getId(),integralTask.getIntegralTaskName());

                }
            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void completeSpendIntegralTask(Long userId, IntegralTaskEnum integralTaskEnum, AssetChangeLogEnum assetChangeLogEnum, BigDecimal spendNum) {
        //查询该任务是否开启
        IntegralTask integralTask= integralTaskDao.getOneTask(integralTaskEnum.getCode());
        //查询首次购买的任务是否开启
        IntegralTask oneTask = integralTaskDao.getOneTask(IntegralTaskEnum.FIRST_CONSUMPTION.getCode());
        //查询用户是否已完成了该任务
        UserReceiveIntegral userReceiveIntegral= userReceiveIntegralDao.getUserReceiveIntegral(userId,integralTaskEnum.getCode());
        //任务为开启状态
        if (ObjectUtil.isNotEmpty(integralTask)){
            //赠送的积分数量
            BigDecimal integralNum = integralTask.getIntegralNum();
            //增加积分
            Boolean aBoolean = userAssetsService.addIntegralByUserId(integralNum, userId, CoinCategoryEnum.INTEGRAL, assetChangeLogEnum,integralTask.getId(),integralTask.getIntegralTaskName());
            if (aBoolean){
                //积分发放成功后插入积分任务领取记录
                //消费兑换等比积分任务每次完成都可以领取,用户完成记录该任务只插入一条数据,userReceiveIntegral不为空时修改用户领取的积分数量即可
                if (ObjectUtil.isNotEmpty(userReceiveIntegral)){
                    UpdateWrapper<UserReceiveIntegral> wrapper=new UpdateWrapper<>();
                    wrapper.set("modify_time",new Date());
                    wrapper.setSql("integral_num = integral_num + "+integralNum);
                    wrapper.setSql("receive_count = receive_count + " +1);
                    wrapper.eq("user_id",userId).eq("integral_task_id",integralTask.getId());
                    wrapper.eq("is_delete",Boolean.FALSE);
                    userReceiveIntegralDao.update(wrapper);
                }else{
                    //第一次购买完成首次购买任务
                    completeOtherIntegralTask(userId,IntegralTaskEnum.FIRST_CONSUMPTION,AssetChangeLogEnum.FIRST_CONSUMPTION);
                    //插入消费等比积分任务完成记录
                    UserReceiveIntegral userReceive=new UserReceiveIntegral();
                    userReceive.setUserId(userId);
                    userReceive.setIntegralNum(integralNum);
                    userReceive.setIntegralTaskId(integralTask.getId());
                    userReceive.setReceiveCount(1);
                    userReceive.setIsReceive(true);
                    userReceive.setReceiveInformation(integralTask.getReceiveInformation());
                    userReceive.setCreateTime(new Date());
                    userReceive.setIsDelete(Boolean.FALSE);
                    userReceive.insert();
                }
            }

        }
    }
}